package service

import (
	"context"
	"errors"
	"patient-srv/basic/config"
	patient "patient-srv/basic/proto"
	"patient-srv/handler/model"

	"gorm.io/gorm"
)

// AddressService 地址服务
type AddressService struct{}

// GetAddressList 获取地址列表
func (s *AddressService) GetAddressList(ctx context.Context, req *patient.GetAddressListRequest) (*patient.AddressListResponse, error) {
	// 调用模型方法获取地址列表
	addresses, err := model.GetAddressListByUserID(config.GlobalDB, req.UserId)
	if err != nil {
		return nil, err
	}

	// 数据转换: model -> proto
	var result []*patient.Address
	for _, addr := range addresses {
		result = append(result, &patient.Address{
			Id:            addr.ID,
			UserId:        addr.UserID,
			Province:      addr.Province,
			City:          addr.City,
			District:      addr.District,
			Address:       addr.Address,
			ReceiverName:  addr.ReceiverName,
			ReceiverPhone: addr.ReceiverPhone,
			IsDefault:     addr.IsDefault,
			CreatedAt:     addr.CreatedAt.Format("2006-01-02 15:04:05"),
			UpdatedAt:     addr.UpdatedAt.Format("2006-01-02 15:04:05"),
		})
	}

	return &patient.AddressListResponse{
		Message:   "获取成功",
		Addresses: result,
	}, nil
}

// AddAddress 添加地址
func (s *AddressService) AddAddress(ctx context.Context, req *patient.AddAddressRequest) (*patient.BaseResponse, error) {
	// 构建地址模型
	address := &model.Address{
		UserID:        req.UserId,
		Province:      req.Province,
		City:          req.City,
		District:      req.District,
		Address:       req.Address,
		ReceiverName:  req.ReceiverName,
		ReceiverPhone: req.ReceiverPhone,
		IsDefault:     req.IsDefault,
	}

	// 调用模型方法创建地址
	if err := address.Create(config.GlobalDB); err != nil {
		return nil, err
	}

	return &patient.BaseResponse{Message: "添加成功"}, nil
}

// UpdateAddress 更新地址
func (s *AddressService) UpdateAddress(ctx context.Context, req *patient.UpdateAddressRequest) (*patient.BaseResponse, error) {
	// 查询地址
	var address model.Address
	err := address.GetByID(config.GlobalDB, req.AddressId, req.UserId)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return &patient.BaseResponse{Message: "地址不存在"}, nil
		}
		return nil, err
	}

	// 准备更新数据
	updates := make(map[string]interface{})
	if req.Province != "" {
		updates["province"] = req.Province
	}
	if req.City != "" {
		updates["city"] = req.City
	}
	if req.District != "" {
		updates["district"] = req.District
	}
	if req.Address != "" {
		updates["address"] = req.Address
	}
	if req.ReceiverName != "" {
		updates["receiver_name"] = req.ReceiverName
	}
	if req.ReceiverPhone != "" {
		updates["receiver_phone"] = req.ReceiverPhone
	}

	// 调用模型方法更新
	if err := address.Update(config.GlobalDB, updates); err != nil {
		return nil, err
	}

	return &patient.BaseResponse{Message: "更新成功"}, nil
}

// DeleteAddress 删除地址
func (s *AddressService) DeleteAddress(ctx context.Context, req *patient.DeleteAddressRequest) (*patient.BaseResponse, error) {
	// 查询地址
	var address model.Address
	err := address.GetByID(config.GlobalDB, req.AddressId, req.UserId)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return &patient.BaseResponse{Message: "地址不存在"}, nil
		}
		return nil, err
	}

	// 调用模型方法删除
	if err := address.Delete(config.GlobalDB, req.UserId); err != nil {
		return nil, err
	}

	return &patient.BaseResponse{Message: "删除成功"}, nil
}

// SetDefaultAddress 设置默认地址
func (s *AddressService) SetDefaultAddress(ctx context.Context, req *patient.SetDefaultAddressRequest) (*patient.BaseResponse, error) {
	// 查询地址
	var address model.Address
	err := address.GetByID(config.GlobalDB, req.AddressId, req.UserId)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return &patient.BaseResponse{Message: "地址不存在"}, nil
		}
		return nil, err
	}

	// 调用模型方法设置默认
	if err := address.SetDefault(config.GlobalDB); err != nil {
		return nil, err
	}

	return &patient.BaseResponse{Message: "设置成功"}, nil
}
